package cn.itcast.rocketmq.spring.ordermsg;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * 描述：在springboot中接收RocketMQ消息
 */
@Component
@RocketMQMessageListener(
        topic = "spring-order-topic",
        consumerGroup = "spring-order-consumer-group",
        selectorExpression = "*",
        consumeMode = ConsumeMode.ORDERLY
)
public class OrderConsumer implements RocketMQListener<MessageExt> {

//    @Override
//    public void onMessage(String msg) {
//        System.out.println("消费时间："+System.currentTimeMillis());
//        System.out.println("接收到消息 -> " + msg);
//    }

    @Override
    public void onMessage(MessageExt msg) {
        try {
            System.out.println(Thread.currentThread().getName() + " "
                    + msg.getQueueId() + " "
                    + new String(msg.getBody(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
